﻿using System;
using System.Drawing;
using System.Drawing.Imaging;
using System.IO;
using ThoughtWorks.QRCode.Codec;

namespace Uranus.Common
{
    public class QRCode
    {
        public static Image GetQRCode(string CodeValue)
        {
            if (!string.IsNullOrEmpty(CodeValue))
            {
                QRCodeEncoder qrCodeEncoder = new QRCodeEncoder();
                //String encoding = cboEncoding.Text;
                //if (encoding == "Byte")
                //{
                    qrCodeEncoder.QRCodeEncodeMode = QRCodeEncoder.ENCODE_MODE.BYTE;
                //}
                //else if (encoding == "AlphaNumeric")
                //{
                //    qrCodeEncoder.QRCodeEncodeMode = QRCodeEncoder.ENCODE_MODE.ALPHA_NUMERIC;
                //}
                //else if (encoding == "Numeric")
                //{
                //    qrCodeEncoder.QRCodeEncodeMode = QRCodeEncoder.ENCODE_MODE.NUMERIC;
                //}
                //try
                //{
                    //int scale = Convert.ToInt16(txtSize.Text);
                    qrCodeEncoder.QRCodeScale = 4;
                //}
                //catch (Exception ex)
                //{
                //    MessageBox.Show("Invalid size!");
                //    return;
                //}
                //try
                //{
                //    int version = Convert.ToInt16(cboVersion.Text);
                    qrCodeEncoder.QRCodeVersion = 7;
                //}
                //catch (Exception ex)
                //{
                //    MessageBox.Show("Invalid version !");
                //}

                //string errorCorrect = cboCorrectionLevel.Text;
                //if (errorCorrect == "L")
                //    qrCodeEncoder.QRCodeErrorCorrect = QRCodeEncoder.ERROR_CORRECTION.L;
                //else if (errorCorrect == "M")
                    qrCodeEncoder.QRCodeErrorCorrect = QRCodeEncoder.ERROR_CORRECTION.M;
                //else if (errorCorrect == "Q")
                //    qrCodeEncoder.QRCodeErrorCorrect = QRCodeEncoder.ERROR_CORRECTION.Q;
                //else if (errorCorrect == "H")
                //    qrCodeEncoder.QRCodeErrorCorrect = QRCodeEncoder.ERROR_CORRECTION.H;

                Image image;
                //String data = txtEncodeData.Text;
                image = qrCodeEncoder.Encode(CodeValue);

                //byte[] byteData = null;

                //using (MemoryStream oMemoryStream = new MemoryStream())
                //{
                //    //建立副本
                //    using (Bitmap oBitmap = new Bitmap(image))
                //    {
                //        //儲存圖片到 MemoryStream 物件，並且指定儲存影像之格式
                //        oBitmap.Save("d:\\" + Guid.NewGuid() + ".png", ImageFormat.Png);
                //        oBitmap.Save(oMemoryStream, ImageFormat.Png);
                //        //設定資料流位置
                //        oMemoryStream.Position = 0;
                //        //設定 buffer 長度
                //        byteData = new byte[oMemoryStream.Length];
                //        //將資料寫入 buffer
                //        oMemoryStream.Read(byteData, 0, Convert.ToInt32(oMemoryStream.Length));
                //        //將所有緩衝區的資料寫入資料流
                //        oMemoryStream.Flush();
                //    }
                //}

                //picEncode.Image = image;
                return image;
            }
            throw  new Exception("请输入需要生成二维码的字符串");
        } 
    }
}